package;

import flixel.system.FlxSound;
import flixel.FlxG;
import flixel.util.FlxColor;
import flixel.FlxSprite;
import flixel.math.FlxPoint;

class Player extends FlxSprite {

    static inline var SPEED:Float = 100;
	var stepSound:FlxSound;
    
    public function new(x:Float = 0, y:Float = 0) {
        super(x, y);
        //makeGraphic(16, 16, FlxColor.BLUE);
        loadGraphic( AssetPaths.player__png, true, 16, 16 );
        drag.x = drag.y = 800;
        
        setSize(8, 8);
        offset.set(4, 4);
        
        setFacingFlip( LEFT, false, false );
        setFacingFlip( RIGHT, true, false );

        animation.add("d_idle", [0]);
        animation.add("lr_idle", [3]);
        animation.add("u_idle", [6]);

        animation.add("d_walk", [0, 1, 0, 2], 6);
        animation.add("lr_walk", [3, 4, 3, 5], 6);
        animation.add("u_walk", [6, 7, 6, 8], 6);
        
		stepSound = FlxG.sound.load(AssetPaths.step__wav);
    }
    
    function updateMovement() {
        var up:Bool = false;
        var down:Bool = false;
        var left:Bool = false;
        var right:Bool = false;

        up = FlxG.keys.anyPressed([UP, W]);
        down = FlxG.keys.anyPressed([DOWN, S]);
        left = FlxG.keys.anyPressed([LEFT, A]);
        right = FlxG.keys.anyPressed([RIGHT, D]);

        if (up && down) {
            up = down = false;
        }

        if (left && right) {
            left = right = false;
        }

        if (up || down || left || right) {
            var newAngle:Float = 0;
            if (up) {
                newAngle = -90;
                if (left) {
                    newAngle -= 45;
                } else if (right) {
                    newAngle += 45;
                }
                facing = UP;
            } else if (down) {
                newAngle = 90;
                if (left) {
                    newAngle += 45;
                } else if (right) {
                    newAngle -= 45;
                }
                facing = DOWN;
            } else if (left) {
                newAngle = 180;
                facing = LEFT;
            } else if (right) {
                newAngle = 0;
                facing = RIGHT;
            }

            velocity.setPolarDegrees(SPEED, newAngle);
        }

        var action = "idle";
        if ((velocity.x != 0 || velocity.y != 0) && touching == NONE) {
			stepSound.play();
            action = "walk";
        }

        switch (facing) {
            case LEFT, RIGHT:
                animation.play("lr_" + action);
            case UP:
                animation.play("u_" + action);
            case DOWN:
                animation.play("d_" + action);
            case _:
        }
    }

    override function update(elapsed:Float) {
        updateMovement();
        super.update(elapsed);
    }
    
}